package com.common.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

@Configuration
public class SwaggerConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
//配置Swagger Docket的bean


    @Bean
    public Docket productApi(Environment environment){
        //设置swagger显示的环境
        //Profiles profiles = Profiles.of("dev","test");
        //boolean flag = environment.acceptsProfiles(profiles);


        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(true)//false 则不能再浏览器中访问
                .select()
                //RequestHandlerSelectors 扫描接口的方式
                //basePackage：指定要扫描的包
                //any():全部
                //none():不扫描
                //withClassAnnotation:扫描类上的注解，参数是一个注解的反射对象
                //withMethodAnnotation :扫描方法上的注解
                .apis(RequestHandlerSelectors.basePackage("com.li.controller"))
                //过滤的路径
                .paths(PathSelectors.ant("/**"))
                .build();
    }

    //配置Swagger 信息 =apiInfo
    private ApiInfo apiInfo(){
        //作者信息
        Contact contact = new Contact("李江","","1617511878@qq.com");
        return new ApiInfo(
                "李江的API",
                "挑战杯项目",
                "1.0",
                "",
                contact,
                "",
                "",
                new ArrayList<>()
        );
    }
}
